package com.mcglib.flac
{
	import com.mcglib.flac.comboBox.DefaultRendererClass;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	
	[Event(name="valueChange",type="com.mcglib.flac.FlacEvent")]
	public class ComboBox extends FlacComponent
	{
		private var _dataProvide:Array = [];
		
		private var _listRendererClass:Class;
		
		private var _defaultListRendererClass:Class = DefaultRendererClass;
		
		private var _downBtn:SimpleButton;
		
		private var _selectedRendererUI:DisplayObject;
		
		private var _selectedIndex:uint = 0;
		
		private var _selectedItem:* = null;
		
		private var _selectedRenderer:IDataRenderer;
		
		private var _listUI:MovieClip;
		
		private var _list:ScrollList;
		
		private var _labelFiled:String;
		
		public function ComboBox(asset:MovieClip,selectedRenderer:IDataRenderer=null,listRendererClass:Class=null)
		{
			super();
			
			_selectedRenderer = selectedRenderer;
			if(_selectedRenderer==null){
				_selectedRenderer = (new _defaultListRendererClass()) as IDataRenderer;
			}
			
			_listRendererClass=listRendererClass?listRendererClass:_defaultListRendererClass;
			
			parseAsset(asset);	
		}
		
		public function get selectedItem():*
		{
			return _selectedItem;
		}

		public function set selectedItem(value:*):void
		{
			if(_dataProvide==null)
				throw "dataProvide为null";
			
			for(var i:int=0;i<_dataProvide.length;i++){
				var item:* = _dataProvide[i];
				if(item==value){
					if(_selectedItem!=value){
						_selectedItem = value;
						_selectedIndex= i;
						var e:FlacEvent = new FlacEvent(FlacEvent.VALUE_CHANGE);
						e.index = i;
						e.value = _selectedItem;
						dispatchEvent(e);
					}
					break;
				}
			}
		}

		public function get labelFiled():String
		{
			return _labelFiled;
		}

		public function set labelFiled(value:String):void
		{
			_labelFiled = value;
		}

		public function get selectedRenderer():IDataRenderer
		{
			return _selectedRenderer;
		}

		public function get selectedIndex():uint
		{
			return _selectedIndex;
		}

		public function set selectedIndex(value:uint):void
		{
			//_selectedIndex = value;
			
			if(_dataProvide==null)
				throw "dataProvide为null";
			
			if(value>=_dataProvide.length){
				value  = _dataProvide.length-1;
			}
			_selectedIndex = value;
			_selectedItem = _dataProvide[_selectedIndex];
			
			var e:FlacEvent = new FlacEvent(FlacEvent.VALUE_CHANGE);
			e.index = value;
			e.value = _selectedItem;
			dispatchEvent(e);
		}

		public function get listRendererClass():Class
		{
			return _listRendererClass;
		}

		public function get dataProvide():Array
		{
			return _dataProvide;
		}

		public function set dataProvide(value:Array):void
		{
			_selectedIndex = -1;
			_selectedItem =  null;
			_dataProvide = value;
			if(value)
				selectedIndex=0;
		}
		
		override public function present(refresh:Boolean=true):void{
			if(_dataProvide==null)
				throw "没设置dataProvide";

			if(_selectedRenderer is DefaultRendererClass){
				(_selectedRenderer as DefaultRendererClass).labelField = _labelFiled;	
			}
			_selectedRenderer.data = _selectedItem;	
		} 

		private function parseAsset(asset:MovieClip):void{
			_downBtn = asset.getChildByName("btn") as SimpleButton;
			_selectedRendererUI = asset.getChildByName("selectedUI");
			_selectedRenderer.initDataRenderer(_selectedRendererUI,this);

			_listUI = asset.getChildByName("list") as MovieClip;
			_list = new ScrollList(_listUI);
			_list.addEventListener(FlacEvent.VALUE_CHANGE,onSelectedChange);
			_list.rendererClass = _listRendererClass;
			_listUI.visible = false;
			
			_downBtn.addEventListener(MouseEvent.CLICK,onDownBtnClick);
		}
		
		private function onSelectedChange(e:FlacEvent):void{
			var index:int = dataProvide.indexOf(e.value);
			selectedIndex = index;
			_listUI.visible = false;
			present();
			
		}
		
		private function onDownBtnClick(e:MouseEvent):void{
			if(!_listUI.visible){
				_list.dataProvide  = getUnselectedDatas();
				_list.present();
				if(_listRendererClass==DefaultRendererClass){
					var ue:FlacEvent = new FlacEvent(FlacEvent.REFRESH);
					ue.value = labelFiled;
					
					_list.dispatchEvent(ue);
				}
				_listUI.visible = true;
				
				if(stage){
					//stage.addEventListener(MouseEvent.MOUSE_DOWN,onStageMouseEvt);
					stage.addEventListener(MouseEvent.MOUSE_UP,onStageMouseEvt);
				}
			}
		}
		
		private function onStageMouseEvt(e:MouseEvent):void{
			var target:DisplayObject = e.target as DisplayObject;
			if(!FlacUtils.contain(target,_listUI)){
				//stage.removeEventListener(MouseEvent.MOUSE_DOWN,onStageMouseEvt);
				stage.removeEventListener(MouseEvent.MOUSE_UP,onStageMouseEvt);
				
				_listUI.visible = false;
			}
		}
		
		
		
		private function get stage():Stage{
			return _listUI.stage;
		}
		
		private function getUnselectedDatas():Array{
			var arr:Array = [];
			for each(var item:* in _dataProvide){
				if(item!=_selectedItem){
					arr.push(item);
				}
			}
			
			return arr;
		}
	}
}